package com.parkinglot.core;

import com.alibaba.fastjson.JSON;
import com.parkinglot.util.HttpUrlutils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class WeChat {
    @Autowired
    HttpUrlutils httpUrlutils;
    public final static String appid = "wxf92e6814d9c36303";
    public final static String secret = "361d9e9d94e42714b1ba47b9d056e5dc";
    String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
    //签名方式
    public static final String SIGNTYPE = "MD5";
    //交易类型
    public static final String TRADETYPE = "JSAPI";
    //微信统一下单接口地址
    public static final String pay_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";



    public Map<String, Object> getopenid(String jscode) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + jscode + "&grant_type=authorization_code";
        StringBuilder sb = new StringBuilder();
        String a = null;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            URL u = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("GET");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            //开始连接
            conn.connect();
            if (conn.getResponseCode() == 200) {
                System.out.println("连接成功");
                // 请求返回的数据
                InputStream in = conn.getInputStream();

                try {
                    byte[] data1 = new byte[in.available()];
                    in.read(data1);
                    // 转成字符串
                    a = new String(data1);
                    map = (Map) JSON.parse(a);
                    map.put("appId", WeChat.appid);
                    map.put("status", "success");
                    System.out.println(map);
                    in.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } catch (Exception e) {
            log.warn("异常对象:{},异常消息:{},异常栈信息:{}", e.getClass(), e.getMessage());
            e.printStackTrace();

        }
        return map;
    }



 /*   public String getopenid(){
        String url="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+jscode+"&grant_type=authorization_code";
        StringBuilder sb=new StringBuilder();
       try {
           URL u=new URL(url);
           URLConnection connection = u.openConnection();
           InputStream inputStream = connection.getInputStream();
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            sb = new StringBuilder();
           while (bufferedReader.readLine() != null) {
               sb.append(bufferedReader.readLine());
           }
       }catch (Exception e){
           log.warn("异常对象:{},异常消息:{},异常栈信息:{}", e.getClass(), e.getMessage());
           e.printStackTrace();

       }
        return sb.toString();
    }*/
}
